<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/27
 * Time: 15:50
 */

namespace app\api\controller;


use app\model\CollectModel;
use app\model\ArticleModel;
use app\model\CategoryModel;
use app\model\UserModel;
use app\model\StaffJurisdictionModel;
use app\model\StaffRoleModel;
use think\facade\Request;
use think\facade\Validate;

class Collect
{
    public function updtcollect()
    {
        // 1. 获取openid
        // 2. 修改收藏状态status = 2就可以了。

        $likeList = Request::param();
        $validate = Validate::rule([
            'open_id|用户id' => 'require',
            'article_id|文章id' => 'require',
        ]);
        if (!$validate->check($likeList)) {
            echo $validate->getError();
            exit();
        }


        $like = CollectModel::where("open_id","=",$likeList['open_id'])
            ->where("article_id","=",$likeList['article_id'])
            ->find();
        if (!$like){
            $likeList['add_time']=$likeList['update_time']=time();
            $result=CollectModel::create($likeList);
        }else{
            if ($like['collect_status']==1){
                $like['collect_status']=2;
            }else{
                $like['collect_status']=1;
            }
//        var_dump($like['like_status']);
            $like['update_time']=time();
//        $likelist=$like->toArray();
            $result = $like->save();
        }
//        var_dump($like->toArray());

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ],
        ];
        return json($data);
    }
    public function findCollect(){
        $likeList=Request::param();
        $validate = Validate::rule([
            'open_id|用户id' => 'require',
            'article_id|文章id' => 'require',
        ]);
        if (!$validate->check($likeList)) {
            echo $validate->getError();
            exit();
        }
        $like = CollectModel::where("open_id","=",$likeList['open_id'])
            ->where("article_id","=",$likeList['article_id'])
            ->find();
        if($like){
            $result=$like->toArray();
            $data = [
                'status' => 0,
                'message' =>'',
                'data' => [
                    'result' =>$result
                ]
        ];
        }else{
            $data = [
                'status' => 10001,
                'message' =>  '查找数据库失败',
                'data' => []
            ];
        }
        return json($data);
    }
    public function collectArticle(){
        $openId = Request::param("open_id");
        $validate = Validate::rule([
            'open_id|用户id' => 'require',
        ]);
        if (!$validate->check(['open_id' => $openId])) {
            $data = [
                'status' => 10001,
                'message' => $validate->getError(),
                'data' => [],
            ];
            return json($data);
        }
        $result = CollectModel::where("open_id", "=", $openId)
            ->where('collect_status','=','1')
            ->select();
        $collectArticleList=[];
        foreach ($result as $item) {
            $collectArticleOne = ArticleModel::where('article_id','=',$item['article_id'])->find();
            $category = CategoryModel::find($collectArticleOne['category_id']);
            $collectArticleOne['category_name'] = $category['category_name'];
            
            $User =UserModel::where("open_id","=",$collectArticleOne['open_id'])->find();
            $collectArticleOne['nickname'] = $User['nickname'];
            
            $commentLength =StaffJurisdictionModel::where("article_id","=",$collectArticleOne['article_id'])->count();
            $collectArticleOne['comment_length'] = $commentLength;
            
            $likeLength =StaffRoleModel::where("article_id","=",$collectArticleOne['article_id'])->where('like_status','=','1')->count();
            $collectArticleOne['like_length'] = $likeLength;
            
            $collectLength =CollectModel::where("article_id","=",$collectArticleOne['article_id'])->where('collect_status','=','1')->count();
            $collectArticleOne['collect_length'] = $collectLength;
            
            $collectArticleList[] = $collectArticleOne;
        }
        
        
        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '查找数据库失败',
            'data' => [
                'collectArticleList' =>$collectArticleList ? $collectArticleList : false
            ],
        ];
        return json($data);
    }
}